﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Configuration;

namespace SCSConfigSystem
{
    public class SCSConfigManagement
    {
        public static string LoadDBConnectionString(string nodeName)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("C:\\SCSCardConfigSystem.config");
            XmlNode node = xmldoc.SelectSingleNode("configuration/connectionStrings/add[@name = '" + nodeName + "']");
            if (node != null)
            {
                XmlNode valueAttribute = node.Attributes.GetNamedItem("connectionString");
                if (valueAttribute != null)
                {
                    return valueAttribute.Value.ToString();
                }
                else
                    return "";
            }
            else
                return "";
        }

        public static string LoadNodeKeyConfig(string nodeKey)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("C:\\SCSCardConfigSystem.config");
            XmlNode node = xmldoc.SelectSingleNode("configuration/appSettings/add[@key = '" + nodeKey + "']");
            if (node != null)
            {
                XmlNode valueAttribute = node.Attributes.GetNamedItem("value");
                if (valueAttribute != null)
                {
                    return valueAttribute.Value.ToString();
                }
                else
                    return "";
            }
            else
                return "";
        }

        public static bool CheckKeyConfigSite(string Keys)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("C:\\SCSCardConfigSystem.config");
            XmlNode node = xmldoc.SelectSingleNode("configuration/appSettings/add[@key = '" + Keys + "']");
            if (node != null)
            {
                return true;
            }
            else
                return false;
        }

        public static void Update(int siteguid, string Values, string Keys)
        {
            //update the config file
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("C:\\SCSCardConfigSystem.config");
            XmlNode node = xmldoc.SelectSingleNode("configuration/appSettings/add[@key = '" + Keys + "']");
            XmlNode valueAttribute = node.Attributes.GetNamedItem("value");
            // save file config
            if (valueAttribute != null)
            {
                valueAttribute.InnerXml = Values;
                xmldoc.Save("C:\\SCSCardConfigSystem.config");
            }

            //update the cached setting
            //System.Configuration.ConfigurationManager.AppSettings.Set("" + Keys + "", Values);
        }

        public static void CreateNode(string Values, string Keys)
        {

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("C:\\SCSCardConfigSystem.config");
            XmlNode appSettingNode = xmldoc.SelectSingleNode("configuration/appSettings");

            //Create a new node
            XmlElement newkey = xmldoc.CreateElement("add");
            newkey.SetAttribute("key", Keys);
            newkey.SetAttribute("value", Values);

            appSettingNode.AppendChild(newkey);

            xmldoc.Save("C:\\SCSCardConfigSystem.config");
        }
    }
}
